<?php

/**
 * ecshop ERP v1.0
 * ----------------------------------------------------------------------------
 * http://www.phpally.com
 * Jacklee的博客 致力于php技术
 * ----------------------------------------------------------------------------
 * 作者: Jacklee
 * 邮箱: jack349392900@gmail.com
 * 创建时间: 2013-11-18
 * 最后修改时间: 2013-11-18
 */

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}

/*
 * 获取出入库记录
 */
function get_stock_record($stock_id='',$stock_style='',$goods_id='',$warehouse_id='',$w_d_style_id='',$start_time='',$end_time='',$start=-1,$num=-1)
{
	if(!empty($stock_id))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_stock')."where stock_id='".$stock_id."'";
	}
	else{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_stock')." where 1 ";
		if(!empty($stock_style))
		{
			$sql.=" and stock_style='".$stock_style."'";
		}
		if(!empty($goods_id))
		{
			$sql.=" and goods_id ='".$goods_id."'";
		}
		if(!empty($warehouse_id))
		{
			$sql.=" and warehouse_id ='".$warehouse_id."'";
		}
		if(!empty($w_d_style_id))
		{
			$sql.=" and w_d_style_id ='".$w_d_style_id."'";
		}
		
		if(!empty($start_time) && empty($end_time))
		{
			$sql.=" and act_time >='".$start_time."'";
		}
		elseif(empty($start_time) && !empty($end_time))
		{
			$sql.=" and act_time <='".$end_time."'";
		}	
		elseif(!empty($start_time) && !empty($end_time))
		{
			$sql.=" and act_time >='".$start_time."' and act_time <='".$end_time."'";
		}
		
		$sql.=" order by stock_id desc ";
		if($start!=-1 && $num!=-1)
		{
			if($start<=0)
			{
				$start=0;
				}
			$sql.=" limit ".$start.",".$num;
		}								
	}
	$res=$GLOBALS['db']->query($sql);
	
	while($row=mysql_fetch_assoc($res))
	{
		$goods_id=$row['goods_id'];
		$sql="select goods_id,cat_id,goods_sn,goods_name,goods_thumb,goods_img,original_img from ".$GLOBALS['ecs']->table('goods')." where goods_id='".$goods_id."'";
		$goods_info=$GLOBALS['db']->getRow($sql);
		//处理图片链接
		if(!empty($goods_info['goods_thumb']))
		{
			$goods_info['goods_thumb']='../'.$goods_info['goods_thumb'];
		}
		if(!empty($goods_info['goods_img']))
		{
			$goods_info['goods_img']='../'.$goods_info['goods_img'];
		}		
		if(!empty($goods_info['original_img']))
		{
			$goods_info['original_img']='../'.$goods_info['original_img'];
		}
		
		$row['goods_info']=$goods_info;
		
		$row['act_date']=date('Y-m-d',$row['act_time']);
		
		$row['goods_attr']=get_attr_info($row['goods_attr_id']);
		
		$warehouse_id=$row['warehouse_id'];
		$sql=" select name from ".$GLOBALS['ecs']->table('erp_warehouse')." where warehouse_id='".$warehouse_id."'";
		$row['warehouse_name']=$GLOBALS['db']->getOne($sql);
		
		$stock_style=$row['stock_style'];
		$w_d_style_id=$row['w_d_style_id'];
		if($stock_style=='w')
		{
			$sql=" select warehousing_style  from ".$GLOBALS['ecs']->table('erp_warehousing_style')." where warehousing_style_id ='".$w_d_style_id."'";
		}
		elseif($stock_style=='d')
		{
			$sql=" select delivery_style  from ".$GLOBALS['ecs']->table('erp_delivery_style')." where delivery_style_id ='".$w_d_style_id."'";
		}
		$row['w_d_style']=$GLOBALS['db']->getOne($sql);
		
		$stock_record[]=$row;
	}
	return $stock_record;
}


/*
 * 清空库存盘点表
 */
function clear_tb_stock_check()
{
	$sql="truncate table ".$GLOBALS['ecs']->table('erp_stock_check');
	$GLOBALS['db']->query($sql);
}

/*
 * 核对某个产品是否已经在库存盘点表里
 */
function in_tb_stock_check($goods_id='',$goods_attr_id='',$warehouse_id='')
{
	if(empty($goods_id) || empty($warehouse_id))
	{
		return false;
	}
	else{
			$sql="select * from ".$GLOBALS['ecs']->table('erp_stock_check')." where goods_id='".$goods_id."' and goods_attr_id='".$goods_attr_id."' and warehouse_id='".$warehouse_id."'";
			return $GLOBALS['db']->getRow($sql);		
	}
}


/*
 * 更新库存盘点表
 */
function update_tb_stock_check($start_time='',$end_time='',$warehouse_id='')
{

	$cls_date=new cls_date();
	
	//清空库存盘点表
	@clear_tb_stock_check();
	
	$current_date=date('Y-m-d',time());
	$first_date=$cls_date->get_first_day($current_date);
	$last_date=$cls_date->get_last_day($current_date);
	
	//如果期末日期为空,默认为当月最后一天
	if(empty($end_time))
	{
		$end_time=$cls_date->date_to_stamp($last_date);
	}
	//如果期初日期为空,默认为当月的1号
	if(empty($start_time))
	{
		$start_time=$cls_date->date_to_stamp($first_date);;
	}	

	
	//处理期初库存
	$sql="select * from ".$GLOBALS['ecs']->table('erp_stock')." where act_time<'".$start_time."'";
	
	if(!empty($warehouse_id))
	{
		$sql.=" and warehouse_id='".$warehouse_id."'";
	}
	
	$stock_info=$GLOBALS['db']->getAll($sql);
	foreach($stock_info as $key => $item)
	{
		$goods_id=$item['goods_id'];
		$goods_attr_id =$item['goods_attr_id'];
		$warehouse_id =$item['warehouse_id'];
		$qty=$item['qty'];
		$stock_style=$item['stock_style'];
		
		$stock_check_item=in_tb_stock_check($goods_id,$goods_attr_id,$warehouse_id);
		//不在库存盘点表里
		if(empty($stock_check_item))
		{
			if($stock_style=='w')
			{
				$sql="insert into ".$GLOBALS['ecs']->table('erp_stock_check')." set goods_id='".$goods_id."',goods_attr_id='".$goods_attr_id."',warehouse_id='".$warehouse_id."',begin_qty='".$qty."'";
				$GLOBALS['db']->query($sql);
			}
			elseif($stock_style=='d')
			{
				$sql="insert into ".$GLOBALS['ecs']->table('erp_stock_check')." set goods_id='".$goods_id."',goods_attr_id='".$goods_attr_id."',warehouse_id='".$warehouse_id."',begin_qty='".$qty."'";
				$GLOBALS['db']->query($sql);				
			}
		}
		else{//在库存盘点表里
			if($stock_style=='w')
			{
				$sql="update ".$GLOBALS['ecs']->table('erp_stock_check')." set begin_qty=begin_qty+'".$qty."' where id='".$stock_check_item['id']."'";
				$GLOBALS['db']->query($sql);
			}
			elseif($stock_style=='d')
			{
				$sql="update ".$GLOBALS['ecs']->table('erp_stock_check')." set begin_qty=begin_qty+'".$qty."' where id='".$stock_check_item['id']."'";
				$GLOBALS['db']->query($sql);			
			}			
		}
	}
	
	//处理出入仓数量
	$sql="select * from ".$GLOBALS['ecs']->table('erp_stock')." where act_time>='".$start_time."' and act_time<='".$end_time."'";
	
	if(!empty($warehouse_id))
	{
		$sql.=" and warehouse_id='".$warehouse_id."'";
	}
	
	$stock_info=$GLOBALS['db']->getAll($sql);
	foreach($stock_info as $key=>$item)
	{
		$goods_id=$item['goods_id'];
		$goods_attr_id =$item['goods_attr_id'];
		$warehouse_id =$item['warehouse_id'];
		$qty=$item['qty'];
		$stock_style=$item['stock_style'];	

		$stock_check_item=in_tb_stock_check($goods_id,$goods_attr_id,$warehouse_id);
		//不在库存盘点表里
		if(empty($stock_check_item))
		{
			if($stock_style=='w')
			{
				$sql="insert into ".$GLOBALS['ecs']->table('erp_stock_check')." set goods_id='".$goods_id."',goods_attr_id='".$goods_attr_id."',warehouse_id='".$warehouse_id."',warehousing_qty='".$qty."'";
				$GLOBALS['db']->query($sql);
			}
			elseif($stock_style=='d')
			{
				$sql="insert into ".$GLOBALS['ecs']->table('erp_stock_check')." set goods_id='".$goods_id."',goods_attr_id='".$goods_attr_id."',warehouse_id='".$warehouse_id."',delivery_qty='".(0-$qty)."'";
				$GLOBALS['db']->query($sql);				
			}
		}
		else{//在库存盘点表里
			if($stock_style=='w')
			{
				$sql="update ".$GLOBALS['ecs']->table('erp_stock_check')." set warehousing_qty=warehousing_qty+'".$qty."' where id='".$stock_check_item['id']."'";
				$GLOBALS['db']->query($sql);
			}
			elseif($stock_style=='d')
			{
				$sql="update ".$GLOBALS['ecs']->table('erp_stock_check')." set delivery_qty=delivery_qty-'".$qty."' where id='".$stock_check_item['id']."'";
				$GLOBALS['db']->query($sql);			
			}			
		}		
	}
	

	//处理期末库存
	$sql="select id,begin_qty,delivery_qty,warehousing_qty from ".$GLOBALS['ecs']->table('erp_stock_check')." where 1";
	
	$stock_check_info=$GLOBALS['db']->getAll($sql);
	
	if(!empty($stock_check_info))
	{
		foreach($stock_check_info as $key=>$item)
		{
			$id=$item['id'];
			$begin_qty=$item['begin_qty'];
			$delivery_qty=$item['delivery_qty'];
			$warehousing_qty=$item['warehousing_qty'];
			
			$end_qty=$begin_qty+$warehousing_qty-$delivery_qty;
			
			$sql="update ".$GLOBALS['ecs']->table('erp_stock_check')." set end_qty='".$end_qty."' where id='".$id."'";
			$GLOBALS['db']->query($sql);
		}
	}
}

/*
 * 获取库存盘点表信息
 */
function get_stock_check_info($start=-1,$num=-1)
{
	$sql="select * from ".$GLOBALS['ecs']->table('erp_stock_check')." where 1 order by goods_id asc";
	if($start!=-1 && $num!=-1)
	{
		if($start<=0)
		{
			$start=0;
			}
		$sql.=" limit ".$start.",".$num;
	}
	$stock_check_info=$GLOBALS['db']->getAll($sql);
	foreach($stock_check_info as $key=>$item)
	{
		$goods_id=$item['goods_id'];
		$warehouse_id=$item['warehouse_id'];
		
		$goods_attr_id=$item['goods_attr_id'];
		$stock_check_info[$key]['goods_attr']=get_attr_info($goods_attr_id);
		
		$sql="select goods_id,cat_id,goods_sn,goods_name,goods_thumb,goods_img,original_img from ".$GLOBALS['ecs']->table('goods')." where goods_id='".$goods_id."'";
		$goods_info=$GLOBALS['db']->getRow($sql);
		//处理图片链接
		if(!empty($goods_info['goods_thumb']))
		{
			$goods_info['goods_thumb']='../'.$goods_info['goods_thumb'];
		}
		if(!empty($goods_info['goods_img']))
		{
			$goods_info['goods_img']='../'.$goods_info['goods_img'];
		}
		if(!empty($goods_info['original_img']))
		{
			$goods_info['original_img']='../'.$goods_info['original_img'];
		}
		
		$stock_check_info[$key]['goods_info']=$goods_info;
		
		$sql="select * from ".$GLOBALS['ecs']->table('erp_warehouse')." where warehouse_id='".$warehouse_id."'";
		$warehouse_info=$GLOBALS['db']->getRow($sql);
		
		$stock_check_info[$key]['warehouse_info']=$warehouse_info;		
	}
	return $stock_check_info;
}

/*
 * 获取产品库存信息
 */
function get_erp_stock($warehouse_id='',$goods_id='',$start=-1,$num=-1)
{	//获取所有 仓库，所有产品的库存信息
	if(empty($warehouse_id) && empty($goods_id))
	{
		$sql="select distinct goods_id from ".$GLOBALS['ecs']->table('erp_stock')." where 1 order by act_time";
		if($start!=-1 && $num!=-1)
		{
			if($start<=0)
			{
				$start=0;
				}
			$sql.=" limit ".$start.",".$num;
		}		
		$res=$GLOBALS['db']->query($sql);
		
		while(list($goods_id)=mysql_fetch_row($res))
		{
			$sql="select goods_id,cat_id,goods_sn,goods_name,goods_thumb,goods_img,original_img from ".$GLOBALS['ecs']->table('goods')." where goods_id='".$goods_id."'";
			$goods_info=$GLOBALS['db']->getRow($sql);
			//处理图片链接
			if(!empty($goods_info['goods_thumb']))
			{
				$goods_info['goods_thumb']='../'.$goods_info['goods_thumb'];
			}
			if(!empty($goods_info['goods_img']))
			{
				$goods_info['goods_img']='../'.$goods_info['goods_img'];
			}
			if(!empty($goods_info['original_img']))
			{
				$goods_info['original_img']='../'.$goods_info['original_img'];
			}
			
			$sql="select distinct warehouse_id from ".$GLOBALS['ecs']->table('erp_stock')." where goods_id='".$goods_id."'";
			$res2=$GLOBALS['db']->query($sql);
			while(list($warehouse_id)=mysql_fetch_row($res2))
			{
				$sql="select name from ".$GLOBALS['ecs']->table('erp_warehouse')." where warehouse_id='".$warehouse_id."'";
				$warehouse_name=$GLOBALS['db']->getOne($sql);

				$sql="select distinct goods_attr_id from ".$GLOBALS['ecs']->table('erp_stock')." where warehouse_id='".$warehouse_id."' and goods_id='".$goods_id."'";
				$res3=$GLOBALS['db']->query($sql);				
				
				while(list($goods_attr_id)=mysql_fetch_row($res3))
				{
					$stock_info[$goods_id]['goods_info']=$goods_info;
					$stock_info[$goods_id]['warehouse'][$warehouse_id]['warehouse_info']['warehouse_id']=$warehouse_id;
					$stock_info[$goods_id]['warehouse'][$warehouse_id]['warehouse_info']['warehouse_name']=$warehouse_name;
					
					$sql="select goods_attr_id,sum(qty) as qty,sum(amount) as amount from ".$GLOBALS['ecs']->table('erp_stock')." where warehouse_id='".$warehouse_id."' and goods_id='".$goods_id."' and goods_attr_id='".$goods_attr_id."' group by warehouse_id";
					$attr_info=$GLOBALS['db']->getRow($sql);
				
					$goods_attr_id=$attr_info['goods_attr_id'];
					$attr_info['attr_value']=get_attr_info($goods_attr_id);
					
		
					$stock_info[$goods_id]['warehouse'][$warehouse_id]['attr'][]=$attr_info;
				}				
				
			}
		}	
	}
	//获取warehouse_id仓库下的所有库存信息
	elseif(!empty($warehouse_id) && empty($goods_id))
	{
		$sql="select distinct goods_id from ".$GLOBALS['ecs']->table('erp_stock')." where 1 order by act_time";
		if($start!=-1 && $num!=-1)
		{
			if($start<=0)
			{
				$start=0;
				}
			$sql.=" limit ".$start.",".$num;
		}		
		$res=$GLOBALS['db']->query($sql);
		while(list($goods_id)=mysql_fetch_row($res))
		{
			$sql="select goods_id,cat_id,goods_sn,goods_name,goods_thumb,goods_img,original_img from ".$GLOBALS['ecs']->table('goods')." where goods_id='".$goods_id."'";
			$goods_info=$GLOBALS['db']->getRow($sql);

			//处理图片链接
			if(!empty($goods_info['goods_thumb']))
			{
				$goods_info['goods_thumb']='../'.$goods_info['goods_thumb'];
			}
			if(!empty($goods_info['goods_img']))
			{
				$goods_info['goods_img']='../'.$goods_info['goods_img'];
			}			
			if(!empty($goods_info['original_img']))
			{
				$goods_info['original_img']='../'.$goods_info['original_img'];
			}	

			$sql="select name from ".$GLOBALS['ecs']->table('erp_warehouse')." where warehouse_id='".$warehouse_id."'";
			$warehouse_name=$GLOBALS['db']->getOne($sql);

				$sql="select distinct goods_attr_id from ".$GLOBALS['ecs']->table('erp_stock')." where warehouse_id='".$warehouse_id."' and goods_id='".$goods_id."'";
				$res3=$GLOBALS['db']->query($sql);				
				
				while(list($goods_attr_id)=mysql_fetch_row($res3))
				{
					$stock_info[$goods_id]['goods_info']=$goods_info;
					$stock_info[$goods_id]['warehouse'][$warehouse_id]['warehouse_info']['warehouse_id']=$warehouse_id;
					$stock_info[$goods_id]['warehouse'][$warehouse_id]['warehouse_info']['warehouse_name']=$warehouse_name;
					
					$sql="select goods_attr_id,sum(qty) as qty,sum(amount) as amount from ".$GLOBALS['ecs']->table('erp_stock')." where warehouse_id='".$warehouse_id."' and goods_id='".$goods_id."' and goods_attr_id='".$goods_attr_id."' group by warehouse_id";
					$attr_info=$GLOBALS['db']->getRow($sql);
				
					$goods_attr_id=$attr_info['goods_attr_id'];
					$attr_info['attr_value']=get_attr_info($goods_attr_id);
					
		
					$stock_info[$goods_id]['warehouse'][$warehouse_id]['attr'][]=$attr_info;
				}	
		}			
	}
	
	elseif(empty($warehouse_id) && !empty($goods_id))
	{
			$sql="select goods_id,cat_id,goods_sn,goods_name,goods_thumb,goods_img,original_img from ".$GLOBALS['ecs']->table('goods')." where goods_id='".$goods_id."'";
			$goods_info=$GLOBALS['db']->getRow($sql);

			//处理图片链接
			if(!empty($goods_info['goods_thumb']))
			{
				$goods_info['goods_thumb']='../'.$goods_info['goods_thumb'];
			}
			if(!empty($goods_info['goods_img']))
			{
				$goods_info['goods_img']='../'.$goods_info['goods_img'];
			}			
			if(!empty($goods_info['original_img']))
			{
				$goods_info['original_img']='../'.$goods_info['original_img'];
			}
			
			$sql="select distinct warehouse_id from ".$GLOBALS['ecs']->table('erp_stock')." where goods_id='".$goods_id."'";
			$res2=$GLOBALS['db']->query($sql);
			while(list($warehouse_id)=mysql_fetch_row($res2))
			{
				$sql="select name from ".$GLOBALS['ecs']->table('erp_warehouse')." where warehouse_id='".$warehouse_id."'";
				$warehouse_name=$GLOBALS['db']->getOne($sql);

				$sql="select distinct goods_attr_id from ".$GLOBALS['ecs']->table('erp_stock')." where warehouse_id='".$warehouse_id."' and goods_id='".$goods_id."'";
				$res3=$GLOBALS['db']->query($sql);				
				
				while(list($goods_attr_id)=mysql_fetch_row($res3))
				{
					$stock_info[$goods_id]['goods_info']=$goods_info;
					$stock_info[$goods_id]['warehouse'][$warehouse_id]['warehouse_info']['warehouse_id']=$warehouse_id;
					$stock_info[$goods_id]['warehouse'][$warehouse_id]['warehouse_info']['warehouse_name']=$warehouse_name;
					
					$sql="select goods_attr_id,sum(qty) as qty,sum(amount) as amount from ".$GLOBALS['ecs']->table('erp_stock')." where warehouse_id='".$warehouse_id."' and goods_id='".$goods_id."' and goods_attr_id='".$goods_attr_id."' group by warehouse_id";
					$attr_info=$GLOBALS['db']->getRow($sql);
				
					$goods_attr_id=$attr_info['goods_attr_id'];
					$attr_info['attr_value']=get_attr_info($goods_attr_id);
					
		
					$stock_info[$goods_id]['warehouse'][$warehouse_id]['attr'][]=$attr_info;
				}				
				
			}			
	}	
	elseif(!empty($warehouse_id) && !empty($goods_id))
	{

			$sql="select goods_id,cat_id,goods_sn,goods_name,goods_thumb,goods_img,original_img  from ".$GLOBALS['ecs']->table('goods')." where goods_id='".$goods_id."'";
			$goods_info=$GLOBALS['db']->getRow($sql);
			
			//处理图片链接
			if(!empty($goods_info['goods_thumb']))
			{
				$goods_info['goods_thumb']='../'.$goods_info['goods_thumb'];
			}
			if(!empty($goods_info['goods_img']))
			{
				$goods_info['goods_img']='../'.$goods_info['goods_img'];
			}			
			if(!empty($goods_info['original_img']))
			{
				$goods_info['original_img']='../'.$goods_info['original_img'];
			}

			$sql="select name from ".$GLOBALS['ecs']->table('erp_warehouse')." where warehouse_id='".$warehouse_id."'";
			$warehouse_name=$GLOBALS['db']->getOne($sql);

				$sql="select distinct goods_attr_id from ".$GLOBALS['ecs']->table('erp_stock')." where warehouse_id='".$warehouse_id."' and goods_id='".$goods_id."'";
				$res3=$GLOBALS['db']->query($sql);				
				
				while(list($goods_attr_id)=mysql_fetch_row($res3))
				{
					$stock_info[$goods_id]['goods_info']=$goods_info;
					$stock_info[$goods_id]['warehouse'][$warehouse_id]['warehouse_info']['warehouse_id']=$warehouse_id;
					$stock_info[$goods_id]['warehouse'][$warehouse_id]['warehouse_info']['warehouse_name']=$warehouse_name;
					
					$sql="select goods_attr_id,sum(qty) as qty,sum(amount) as amount from ".$GLOBALS['ecs']->table('erp_stock')." where warehouse_id='".$warehouse_id."' and goods_id='".$goods_id."' and goods_attr_id='".$goods_attr_id."' group by warehouse_id";
					$attr_info=$GLOBALS['db']->getRow($sql);
				
					$goods_attr_id=$attr_info['goods_attr_id'];
					$attr_info['attr_value']=get_attr_info($goods_attr_id);
					
		
					$stock_info[$goods_id]['warehouse'][$warehouse_id]['attr'][]=$attr_info;
				}	

	}		
	
	return $stock_info;
}


/*
 * 判断出仓类型是否存在，如存在返回出仓类型信息，否则返回false
 */
function is_delivery_style_exists($delivery_style_id='')
{
	if(empty($delivery_style_id))
	{
		return false;
	}
	else{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_delivery_style')." where delivery_style_id='".$delivery_style_id."'";
		$result=$GLOBALS['db']->getRow($sql);
		if($result!=false)
		{
			return $result;
		}
		else{
			return false;
		}
	}
}

/*
 * 获取出仓类型信息
 */

function get_delivery_style_info($delivery_style_id='',$is_valid='')
{
	if(empty($delivery_style_id))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_delivery_style');
		
		if(!empty($is_valid))
		{
			$sql.=" where is_valid='".$is_valid."'";
		}
		
		return $GLOBALS['db']->getAll($sql);
	}
	else{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_delivery_style');
		$sql.=" where delivery_style_id='".$delivery_style_id."'";
		
		return $GLOBALS['db']->getRow($sql);
	}
}

/**
 * 获取出库单信息。
 * 默认返回所有出库单信息
 */
function get_delivery_info($delivery_id='',$delivery_sn='',$status='',$start_time='',$end_time='',$delivery_style_id='',$warehouse_id='',$delivery_to='',$start=-1,$num=-1)
{
	if(empty($delivery_id) && empty($delivery_sn))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_delivery')." where 1";
		
		if(!empty($status))
		{
			$sql.=" and status='".$status."' ";
		}
		if(!empty($start_time) && empty($end_time))
		{
			$sql.=" and create_time>='".$start_time."'";
		}
		elseif(empty($start_time) && !empty($end_time))
		{
			$sql.=" and create_time<='".$end_time."'";
		}	
		elseif(!empty($start_time) && !empty($end_time))
		{
			$sql.=" and create_time>='".$start_time."' and create_time<='".$end_time."'";
		}	
		if(!empty($delivery_style_id))
		{
			$sql.=" and delivery_style_id='".$delivery_style_id."'";
		}
		if(!empty($warehouse_id))
		{
			$sql.=" and warehouse_id='".$warehouse_id."'";
		}
		if(!empty($delivery_to))
		{
			$sql.=" and delivery_to='".$delivery_to."'";
		}
		
		$sql.=" order by delivery_sn desc";
		
		if($start!=-1 && $num!=-1)
		{
			if($start<=0)
			{
				$start=0;
			}
			$sql.=" limit ".$start.",".$num;
		}
		
						
	}
	elseif(!empty($delivery_id) && empty($delivery_sn))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_delivery')." where delivery_id='".$delivery_id."'";
	}
	elseif(empty($delivery_id) && !empty($delivery_sn))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_delivery')." where delivery_sn='".$delivery_sn."'";
	}	
	
	$res=$GLOBALS['db']->query($sql);
	while($row=mysql_fetch_assoc($res))
	{
		$create_time=$row['create_time'];
		$row['delivery_date']=date('Y-m-d',$create_time);
				
		
		//获取可用操作
		$delivery_id=$row['delivery_id'];
		$row['available_act']=get_available_act($delivery_id,'delivery');
			
		//获取出库类型
		$delivery_style_id=$row['delivery_style_id'];
		$sql="select * from ".$GLOBALS['ecs']->table('erp_delivery_style')." where delivery_style_id='".$delivery_style_id."'";
		$row['delivery_style_info']=$GLOBALS['db']->getRow($sql);
			
		//获取仓库信息
		$warehouse_id=$row['warehouse_id'];
		$sql="select * from ".$GLOBALS['ecs']->table('erp_warehouse')." where warehouse_id='".$warehouse_id."'";
		$row['warehouse_info']=$GLOBALS['db']->getRow($sql);
			
		//获取出仓操作者信息
		$operator_id=$row['create_by'];
		$sql="select user_name from ".$GLOBALS['ecs']->table('admin_user')." where user_id='".$operator_id."'";
		$row['admin_name']=$GLOBALS['db']->getOne($sql);
			
		//获取提交出库单操作者信息
		if(!empty($row['post_by']))
		{
			$post_by=$row['post_by'];
			$sql="select user_name from ".$GLOBALS['ecs']->table('admin_user')." where user_id='".$post_by."'";
			$row['post_by_name']=$GLOBALS['db']->getOne($sql);						
		}
		//获取审核入库单操作者信息
		if(!empty($row['approved_by']))
		{
			$approved_by=$row['approved_by'];
			$sql="select user_name from ".$GLOBALS['ecs']->table('admin_user')." where user_id='".$approved_by."'";
			$row['approved_by_name']=$GLOBALS['db']->getOne($sql);						
		}	
			
		//获取关联的订单信息
		if(!empty($row['order_id']))
		{
			$order_id=$row['order_id'];
			$sql="select order_id,order_sn,user_id from ".$GLOBALS['ecs']->table('order_info')." where order_id='".$order_id."'";
			$row['order_info']=$GLOBALS['db']->getRow($sql);		
		}			
			
		$delivery_info[]=$row;
	}
	return $delivery_info;	
}


/**
 * 获取产品类别
 */
function get_cat_info($cat_id='')
{
	if(empty($cat_id))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('category')." where 1";
		return $GLOBALS['db']->getAll($sql);
	}
	else{
		$sql="select * from ".$GLOBALS['ecs']->table('category')." where cat_id='".$cat_id."'";
		return $GLOBALS['db']->getAll($sql);
	}
}


/**
 * 获取产品信息
 * $start起始偏移量
 * $num 获取几条数据
 * $cat_id产品分类
 * $sort,按什么排序
 * $sort_type升序还是降序
 */

function get_goods_info($goods_id='',$goods_sn='',$start=-1,$num=-1,$cat_id='',$sort='',$sort_type='')
{
	//获取所有产品信息
	if(empty($goods_id) && empty($goods_sn))
	{
		$sql="select goods_id,cat_id,goods_sn,goods_name,goods_number,goods_thumb,goods_img,original_img,warn_number from ".$GLOBALS['ecs']->table('goods');
		$sql.=" where is_delete='0' and is_real='1'"; 
		
		if(!empty($cat_id))
		{
			$sql.=" and cat_id='".$cat_id."' ";
		}
		
		if(!empty($sort))
		{
			$sort=trim($sort);
			switch($sort)
			{
				case 'goods_id':
					$sql.=" order by goods_id ";
					break;
				case 'goods_sn':
					$sql.=" order by goods_sn ";
					break;					
				case 'goods_name':
					$sql.=" order by goods_name ";
					break;	
				case 'goods_number':
					$sql.=" order by goods_number ";
					break;														
				case 'last_update':
					$sql.=" order by last_update ";
					break;	
				case 'add_time':
					$sql.=" order by add_time ";
					break;						
			}
		}
		else{
			$sql.=" order by last_update ";
		}
		
		if(!empty($sort_type))
		{
			$sort_type=trim($sort_type);
			if($sort_type='desc')
			{
				$sql.=" desc";
			}
			elseif($sort_type='asc')
			{
				$sql.=" asc";
			}
		}
		else
		{
			$sql.=" desc";
		}
		
		if($start!=-1 && $num!=-1)
		{
			if($start<=0)
			{
				$start=0;
			}
			$sql.=" limit ".$start.",".$num;
		}
	}

	elseif(!empty($goods_id) && empty($goods_sn))
	{
		$sql="select goods_id,cat_id,goods_sn,goods_name,goods_number,goods_thumb,goods_img,original_img,warn_number from ".$GLOBALS['ecs']->table('goods');
		$sql.=" where goods_id='".$goods_id."' and is_delete='0'"; 

	}
	elseif(empty($goods_id) && !empty($goods_sn))
	{
		$sql="select goods_id,cat_id,goods_sn,goods_name,goods_number,goods_thumb,goods_img,original_img,warn_number from ".$GLOBALS['ecs']->table('goods');
		$sql.=" where goods_sn='".$goods_sn."' and is_delete='0'"; 
	}
	else{
		return false;
	}

	$goods_info=$GLOBALS['db']->getAll($sql);
	
	if(!empty($goods_info))
	{
		foreach($goods_info as $key => $goods)
		{
			$goods_id=$goods['goods_id'];
			
			$sql="select * from ".$GLOBALS['ecs']->table('erp_goods_attr')." where goods_id='".$goods_id."'";
			$goods_attr=$GLOBALS['db']->getAll($sql);

			$total_qty=0;
			
			if(!empty($goods_attr))
			{
				foreach($goods_attr as $index => $item)
				{
					$total_qty+=$item['goods_qty'];
					
					$attr_id=$item['attr_id'];
					$goods_attr_id_list=$item['goods_attr_id_list'];
					
					if($goods_attr_id_list!='$$')
					{
						$goods_attr[$index]['attr_value']=get_attr_info($attr_id);
					}
					else{
						$goods_attr[$index]['attr_value']=array();						
					}
				}
			}			
			
			$goods_info[$key]['goods_attr']=$goods_attr;
			$goods_info[$key]['total_qty']=$total_qty;
		}
	}

		//处理图片链接
		if(!empty($goods_info))
		{
			foreach($goods_info as $key => $item)
			{
				if(!empty($item['goods_thumb']))
				{
					$goods_info[$key]['goods_thumb']='../'.$item['goods_thumb'];
				}
				if(!empty($item['goods_img']))
				{
					$goods_info[$key]['goods_img']='../'.$item['goods_img'];
				}			
				if(!empty($item['original_img']))
				{
					$goods_info[$key]['original_img']='../'.$item['original_img'];
				}
			}
		}
		return $goods_info;		
}

/**
 * 检验条码是否是合法的条码，产品货号，尺码是否正确
 * 合法的条码是形如'$xxx-xx-xx*'的字符串，以'$'开头，以'*'结尾，最后一个'-'分隔产品货号和尺码， 产品货号和尺码最短为1位。
 * 如果条码不合法，返回1，如果产品货号不正确，返回2，如果产品尺码不正确，返回3
 * 如果条码合法，产品货号和尺码都正确，返回含有产品货号，尺码的数组
 */
function check_barcode($barcode='')
{
	if(empty($barcode))
	{
		return 1;
	}
	else{
		//去掉可能的前后空格
		$barcode=trim($barcode);
		//首字母应该是'$',最后一个字母应该是'*'
		$first_char=substr($barcode,0,1);
		$last_char=substr($barcode,-1,1);
		
		if($first_char!='$')
		{
			return 1;
		}
		if($last_char!='*')
		{
			return 1;
		}
		//获取最后一个'-'的位置
		$pos=strripos($barcode,'-');
		//如果找不到'-'(如$200238*)，或位置为1(表示产品货号为空，如'$-38*')，那么是非法的条码。
		if($pos==false || $pos==1)
		{
			return 1;
		}
		else{
			//产品货号
			$goods_sn=substr($barcode,1,$pos-1);
			//产品尺码
			$goods_size=substr($barcode,$pos+1,strlen($barcode)-$pos-2);
			//如果产品尺码为空
			if(empty($goods_size))
			{
				return 1;
			}
		}
		
		//核对产品货号是否正确
		if(!check_goods_sn($goods_sn))
		{
			return 2;
		}
		//核对产品尺码是否正确
		if(!check_goods_size('',$goods_sn,$goods_size))
		{
			return 3;
		}		
		
		$goods_info['goods_sn']=$goods_sn;
		$goods_info['goods_size']=$goods_size;
				
		return $goods_info;
	}
}


/**
 * 检验文件是否是合法的入库文件
 * 合法的入库文件格式是这样的：文本文件，条码，逗号，回车，下一个条码，逗号，回车，依此类推
 * 并且，入库文件的每一项的产品货号，尺码都是正确的。
 * 
 * 合法返回含产品货号，尺码，数量的数值，否则，如果文件不合法，返回1
 */
function check_file($souce_file)
{
	if(empty($souce_file))
	{
		return 1;
	}
	else{		
		$souce_file=str_replace("\\", "/", $souce_file);
		if(filesize($souce_file)==0)
		{
			return 1;
		}
		$file_handle=fopen($souce_file,'r');
		$contents = fread($file_handle, filesize($souce_file));
		//把回车替换成'|'
		$contents=preg_replace("/([\r\n]+)/",'|',$contents);
		//生成数组
		$contents_array=explode('|',$contents);
		
		foreach($contents_array as $key=>$array_item)
		{
			if(!empty($array_item))
			{
					$array_item=explode(',',$array_item);
					if(count($array_item)!=2)
					{
						return 1;
					}
					else{
						$qty=$array_item[1];
						if(!ereg('^[0-9]*[1-9][0-9]*$',$qty))
						{
							return 1;
						}						
						
						$barcode=$array_item[0];
						$goods_info=check_barcode($barcode);
						if(!is_array($goods_info))
						{
							return $goods_info;
						}

						$warehousing_info[$key]['goods_info']=$goods_info;
						$warehousing_info[$key]['qty']=$qty;
					}									
			}				
		}
		
		return $warehousing_info;
	}
}

/**
 * 转移并重新命名上传的文件
 */
function format_file($souce_file)
{
	//扩展名
	$file_ext = 'txt';
	//生成文件名
	$file_name=get_current_date().'-'.mt_rand(0,10000).'.'.$file_ext;
	
	$dir='./upload_files/batch_warehousing/';
	
	if (!make_dir($dir))
    {
        return false;
    }
	
	if(!move_uploaded_file($souce_file,$dir.$file_name))
	{
		return false;
	}
	else{
		return $dir.$file_name;
	}
}



/**
 * 检查出库单是否完善，检查的内容有：
 * 1，如果是关联订单出库，是否已选择订单
 * 2，是否已输入出库人
 * 3，是否最少有一个出库单项目
 * 4，每条入库单项的出库数是否大于0
 */

function check_delivery_integrity($delivery_id='')
{
	if(empty($delivery_id))
	{
		return false;
	}
	$sql="select delivery_style_id,delivery_to,order_id from ".$GLOBALS['ecs']->table('erp_delivery')." where delivery_id='".$delivery_id."'";
	$res=$GLOBALS['db']->getRow($sql);
	
	$delivery_style_id=$res['delivery_style_id'];
	$delivery_to=$res['delivery_to'];
	$order_id=$res['order_id'];
	
	if($delivery_style_id==1)
	{
		if(empty($order_id))
		{
			return 1;
		}	
	}
	
	if(empty($delivery_to))
	{
		return 2;
	}
	
	$sql="select delivery_item_id,delivered_qty  from ".$GLOBALS['ecs']->table('erp_delivery_item')." where delivery_id=".$delivery_id."";
	$delivery_item_info=$GLOBALS['db']->getAll($sql);
	if(empty($delivery_item_info))
	{
		return 3;
	}
	else{
		foreach($delivery_item_info as $delivery_item)
		{
			if($delivery_item['delivered_qty']<=0)
			{
				return 4;
			}
		}
	}
	
	return 0;
}



/**
 * 检查入库单是否完善，检查的内容有：
 * 1，如果是关联订单入库，是否已选择订单
 * 2，是否已输入入库人
 * 3，是否最少有一个入库单项目
 * 4，每条入库单项的入库数是否大于0
 */

function check_warehousing_integrity($warehousing_id='')
{
	if(empty($warehousing_id))
	{
		return false;
	}
	$sql="select warehousing_style_id,warehousing_from,order_id from ".$GLOBALS['ecs']->table('erp_warehousing')." where warehousing_id='".$warehousing_id."'";
	$res=$GLOBALS['db']->getRow($sql);
	
	$warehousing_style_id=$res['warehousing_style_id'];
	$warehousing_from=$res['warehousing_from'];
	$order_id=$res['order_id'];
	
	if($warehousing_style_id==1)
	{
		if(empty($order_id))
		{
			return 1;
		}	
	}
	
	if(empty($warehousing_from))
	{
		return 2;
	}
	
	$sql="select warehousing_item_id,received_qty  from ".$GLOBALS['ecs']->table('erp_warehousing_item')." where warehousing_id=".$warehousing_id."";
	$warehousing_item_info=$GLOBALS['db']->getAll($sql);
	if(empty($warehousing_item_info))
	{
		return 3;
	}
	else{
		foreach($warehousing_item_info as $warehousing_item)
		{
			if($warehousing_item['received_qty']<=0)
			{
				return 4;
			}
		}
	}
	
	return 0;
}

/**
 * 检查产品货号是否有效
 */
function check_goods_sn($goods_sn='')
{
	if(empty($goods_sn))
	{
		return false;
	}
	else{
		$sql="select goods_id from ".$GLOBALS['ecs']->table('goods')." where goods_sn='".$goods_sn."' and is_delete='0'";
		$result=$GLOBALS['db']->getRow($sql);
		if(!empty($result))
		{
			return true;
		}
		else{
			return false;
		}
	}
}


/**
 * 新建入库单
 */
function add_delivery()
{
	$delivery_sn=gen_delivery_sn();
	$admin_id=erp_get_admin_id();

	$warehouse_info=get_warehouse_info('',$admin_id,1);
	
	if(empty($warehouse_info))
	{
		return -1;
	}
	else
	{
		$warehouse_id=$warehouse_info[0]['warehouse_id'];
	
		$sql="insert into ".$GLOBALS['ecs']->table('erp_delivery')." set ";
		$sql.="delivery_sn='".$delivery_sn."',";
		$sql.="delivery_style_id='1',";
		$sql.="warehouse_id ='".$warehouse_id."',";
		$sql.="create_time='".time()."',";
		$sql.="create_by='".$admin_id."',";
		$sql.="delivery_to  ='',";
		$sql.="order_id  ='',";
		$sql.="status   ='1'";
		
		if($GLOBALS['db']->query($sql))
		{		
			$sql="select delivery_id from ".$GLOBALS['ecs']->table('erp_delivery')." where delivery_sn='".$delivery_sn."'";
			return $GLOBALS['db']->getOne($sql);		
		}
		else{
			return false;
		}
	}	
}

/**
 * 新建入库单
 */
function add_warehousing()
{
	$warehousing_sn=gen_warehousing_sn();
	$admin_id=erp_get_admin_id();

	$warehouse_info=get_warehouse_info('',$admin_id,1);
	
	if(empty($warehouse_info))
	{
		return -1;
	}
	else
	{
		$warehouse_id=$warehouse_info[0]['warehouse_id'];
	
		$sql="insert into ".$GLOBALS['ecs']->table('erp_warehousing')." set ";
		$sql.="warehousing_sn='".$warehousing_sn."',";
		$sql.="warehousing_style_id='1',";
		$sql.="warehouse_id ='".$warehouse_id."',";
		$sql.="create_time='".time()."',";
		$sql.="create_by='".$admin_id."',";
		$sql.="warehousing_from  ='',";
		$sql.="order_id  ='',";
		$sql.="status   ='1'";
		
		if($GLOBALS['db']->query($sql))
		{		
			$sql="select warehousing_id from ".$GLOBALS['ecs']->table('erp_warehousing')." where warehousing_sn='".$warehousing_sn."'";
	 		return $GLOBALS['db']->getOne($sql);	
		}
		else{
			return false;
		}
	}
}


/**
 * 建立出库单项
 */
function create_delivery_item_by_order($order_id,$delivery_id)
{
	if(empty($order_id)||empty($delivery_id))
	{
		return false;
	}
	else{
		
		$sql="select warehouse_id from ".$GLOBALS['ecs']->table('erp_delivery')." where delivery_id='".$delivery_id."'";
		$warehouse_id=$GLOBALS['db']->getOne($sql);
		
		$sql="select rec_id,goods_id,goods_price,(goods_number-delivery_qty) as expected_qty,goods_attr_id from ".$GLOBALS['ecs']->table('order_goods');
		$sql.=" where order_id='".$order_id."' and goods_number>delivery_qty";
		
		$res=$GLOBALS['db']->query($sql);
		
		while(list($rec_id,$goods_id,$goods_price,$expected_qty,$goods_attr_id)=mysql_fetch_row($res))
		{
			if(!empty($goods_attr_id))
			{
				$attr_id=get_attr_id($goods_id,$goods_attr_id);
				
				$goods_stock=get_goods_stock($warehouse_id,$goods_id,$attr_id);
				
				if($goods_stock>=$expected_qty)
				{
					$sql="insert into ".$GLOBALS['ecs']->table('erp_delivery_item')." set delivery_id='".$delivery_id."'";
					$sql.=",goods_id='".$goods_id."',attr_id='".$attr_id."',expected_qty='".$expected_qty."',price='".$goods_price."',order_item_id='".$rec_id."'";						
					$GLOBALS['db']->query($sql);
				}
				else{
					return false;
				}
			}
			else{
				$goods_stock=get_goods_stock($warehouse_id,$goods_id,'');

				if($goods_stock>=$expected_qty)
				{
					$sql="insert into ".$GLOBALS['ecs']->table('erp_delivery_item')." set delivery_id='".$delivery_id."'";
					$sql.=",goods_id='".$goods_id."',expected_qty='".$expected_qty."',price='".$goods_price."',order_item_id='".$rec_id."'";						
					$GLOBALS['db']->query($sql);
				}
				else{
					return false;
				}						
			}
		}
		
		return true;
		}
}

/**
 * 建立入库单项
 */
function create_warehousing_item_by_order($order_id,$warehousing_id)
{
	if(empty($order_id)||empty($warehousing_id))
	{
		return false;
	}
	else{
		$sql="select order_item_id,goods_id,attr_id,(order_qty-warehousing_qty) as expected_qty,price from ".$GLOBALS['ecs']->table('erp_order_item')." where order_id='".$order_id."' and order_qty>warehousing_qty";

		$res=$GLOBALS['db']->query($sql);
		
		while(list($order_item_id,$goods_id,$attr_id,$expected_qty,$price)=mysql_fetch_row($res))
		{			
			$sql="insert into ".$GLOBALS['ecs']->table('erp_warehousing_item')." set warehousing_id='".$warehousing_id."'";
			$sql.=",goods_id='".$goods_id."',attr_id='".$attr_id."',expected_qty='".$expected_qty."',price='".$price."',account_payable=0,paid_amount=0,order_item_id='".$order_item_id."'";
			
			$GLOBALS['db']->query($sql);
		}
		
		return true;
	}
}

/**
 * 判断销售订单是否存在，是否已被删除，存在返回true，否则返回false;
 */

function is_sales_order_exists($order_id='',$order_sn='')
{
	if(empty($order_id) && empty($order_sn))
	{
		return false;
	}
	elseif(!empty($order_id) && empty($order_sn))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('order_info')." where order_id='".$order_id."'";
		$res=$GLOBALS['db']->getRow($sql);
		
		if($res==false || $res['is_deleted']==1)
		{
			return false;
		}
		else
		{
			return $res;
		}
	}
	elseif(empty($order_id) && !empty($order_sn))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('order_info')." where order_sn ='".$order_sn."'";
		$res=$GLOBALS['db']->getRow($sql);
		
		if($res==false || $res['is_deleted']==1)
		{
			return false;
		}
		else
		{
			return $res;
		}		
	}	
}

/**
 * 判断购买订单是否存在，是否已被删除，存在返回true，否则返回false;
 */

function is_order_exists($order_id='',$order_sn='')
{
	if(empty($order_id) && empty($order_sn))
	{
		return false;
	}
	elseif(!empty($order_id) && empty($order_sn))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_order')." where order_id='".$order_id."'";
		$res=$GLOBALS['db']->getRow($sql);
		
		if($res==false || $res['is_deleted']==1)
		{
			return false;
		}
		else
		{
			return $res;
		}
	}
	elseif(empty($order_id) && !empty($order_sn))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_order')." where order_sn ='".$order_sn."'";
		$res=$GLOBALS['db']->getRow($sql);
		
		if($res==false || $res['is_deleted']==1)
		{
			return false;
		}
		else
		{
			return $res;
		}		
	}	
}


/*
 * 判断出仓单项是否存在，如存在返回出仓单项信息，否则返回false
 */
function is_delivery_item_exists($delivery_item_id='')
{
	if(empty($delivery_item_id))
	{
		return false;
	}
	else{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_delivery_item')." where delivery_item_id='".$delivery_item_id."'";
		$result=$GLOBALS['db']->getRow($sql);
		if($result!=false)
		{
			return $result;
		}
		else{
			return false;
		}
	}
}

/*
 * 判断入仓单项是否存在，如存在返回入仓单项信息，否则返回false
 */
function is_warehousing_item_exists($warehousing_item_id='')
{
	if(empty($warehousing_item_id))
	{
		return false;
	}
	else{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_warehousing_item')." where warehousing_item_id='".$warehousing_item_id."'";
		$result=$GLOBALS['db']->getRow($sql);
		if($result!=false)
		{
			return $result;
		}
		else{
			return false;
		}
	}
}

/*
 * 判断出仓单是否存在，如存在返回出仓单信息，否则返回false
 */
function is_delivery_exists($delivery_id='')
{
	if(empty($delivery_id))
	{
		return false;
	}
	else{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_delivery')." where delivery_id='".$delivery_id."'";
		$result=$GLOBALS['db']->getRow($sql);
		if($result!=false)
		{
			return $result;
		}
		else{
			return false;
		}
	}
}

/*
 * 判断入仓单是否存在，如存在返回入仓单信息，否则返回false
 */
function is_warehousing_exists($warehousing_id='')
{
	if(empty($warehousing_id))
	{
		return false;
	}
	else{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_warehousing')." where warehousing_id='".$warehousing_id."'";
		$result=$GLOBALS['db']->getRow($sql);
		if($result!=false)
		{
			return $result;
		}
		else{
			return false;
		}
	}
}


/**
 * 获取出库单项信息。
 */

/**
 * 获取入库单项信息。
 */
function get_delivery_item_info($delivery_id='')
{
	if(empty($delivery_id))
	{
		return false;
	}
	else
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_delivery_item')." where delivery_id='".$delivery_id."'";
		$delivery_item_info=$GLOBALS['db']->getAll($sql);
	
		if(!empty($delivery_item_info))
		{
			foreach($delivery_item_info as $key => $item)
			{
				//获取产品信息
				$goods_id=$item['goods_id'];
				
				$sql="select cat_id,goods_id,goods_sn,goods_name,goods_thumb,goods_img from ";
				$sql.=$GLOBALS['ecs']->table('goods')." where goods_id='".$goods_id."'";
				
				$goods_details=$GLOBALS['db']->getRow($sql);
				//处理产品图片
				if(!empty($goods_details['goods_thumb']))
				{
					$goods_details['goods_thumb']='../'.$goods_details['goods_thumb'];
				}
				if(!empty($goods_details['goods_img']))
				{
					$goods_details['goods_img']='../'.$goods_details['goods_img'];
				}
				
				$delivery_item_info[$key]['goods_details']=$goods_details;
				
				$delivery_item_info[$key]['goods_attr']=get_goods_attr($item['goods_id']);
				
				$attr_info=get_attr_info($item['attr_id']);
				
				$delivery_item_info[$key]['selected_attr']=$attr_info['attr_info'];				
			}
		}
	
		return $delivery_item_info;
	}
}

/**
 * 获取入库单项信息。
 */
function get_warehousing_item_info($warehousing_id='')
{
	if(empty($warehousing_id))
	{
		return false;
	}
	else
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_warehousing_item')." where warehousing_id='".$warehousing_id."'";
		$warehousing_item_info=$GLOBALS['db']->getAll($sql);
	
		if(!empty($warehousing_item_info))
		{
			foreach($warehousing_item_info as $key => $item)
			{
				//获取产品信息
				$goods_id=$item['goods_id'];
				
				$sql="select cat_id,goods_id,goods_sn,goods_name,goods_thumb,goods_img from ";
				$sql.=$GLOBALS['ecs']->table('goods')." where goods_id='".$goods_id."'";
				
				$goods_details=$GLOBALS['db']->getRow($sql);
				//处理产品图片
				if(!empty($goods_details['goods_thumb']))
				{
					$goods_details['goods_thumb']='../'.$goods_details['goods_thumb'];
				}
				if(!empty($goods_details['goods_img']))
				{
					$goods_details['goods_img']='../'.$goods_details['goods_img'];
				}
				
				$warehousing_item_info[$key]['goods_details']=$goods_details;
				
				$warehousing_item_info[$key]['goods_attr']=get_goods_attr($item['goods_id']);
				
				$attr_info=get_attr_info($item['attr_id']);
				
				$warehousing_item_info[$key]['selected_attr']=$attr_info['attr_info'];				
			}
		}
	
		return $warehousing_item_info;
	}
}


/**
 * 获取入库单信息。
 * 默认返回所有入库单信息
 */
function get_warehousing_info($warehousing_id='',$warehousing_sn='',$status='',$start_time='',$end_time='',$warehousing_style_id='',$warehouse_id='',$warehousing_from='',$order_id='',$start=-1,$num=-1)
{
	if(empty($warehousing_id) && empty($warehousing_sn))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_warehousing')." where 1";
		
		if(!empty($status))
		{
			$sql.=" and status='".$status."' ";
		}
		if(!empty($start_time) && empty($end_time))
		{
			$sql.=" and create_time>='".$start_time."'";
		}
		elseif(empty($start_time) && !empty($end_time))
		{
			$sql.=" and create_time<='".$end_time."'";
		}	
		elseif(!empty($start_time) && !empty($end_time))
		{
			$sql.=" and create_time>='".$start_time."' and create_time<='".$end_time."'";
		}	
		if(!empty($warehousing_style_id))
		{
			$sql.=" and warehousing_style_id='".$warehousing_style_id."'";
		}
		if(!empty($warehouse_id))
		{
			$sql.=" and warehouse_id='".$warehouse_id."'";
		}
		if(!empty($warehousing_from))
		{
			$sql.=" and warehousing_from='".$warehousing_from."'";
		}
		if(!empty($order_id))
		{
			$sql.=" and order_id='".$order_id."'";
		}
		
		$sql.=" order by warehousing_sn desc";
		
		if($start!=-1 && $num!=-1)
		{
			if($start<=0)
			{
				$start=0;
			}
			$sql.=" limit ".$start.",".$num;
		}
		
						
	}
	elseif(!empty($warehousing_id) && empty($warehousing_sn))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_warehousing')." where warehousing_id='".$warehousing_id."'";
	}
	elseif(empty($warehousing_id) && !empty($warehousing_sn))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_warehousing')." where warehousing_sn='".$warehousing_sn."'";
	}	
	
	$res=$GLOBALS['db']->query($sql);
	while($row=mysql_fetch_assoc($res))
	{
		$create_time=$row['create_time'];
		$row['warehousing_date']=date('Y-m-d',$create_time);
		
		
		//获取可用操作
		$warehousing_id=$row['warehousing_id'];
		$row['available_act']=get_available_act($warehousing_id,'warehousing');
			
		//获取入库类型
		$warehousing_style_id=$row['warehousing_style_id'];
		$sql="select * from ".$GLOBALS['ecs']->table('erp_warehousing_style')." where warehousing_style_id='".$warehousing_style_id."'";
		$row['warehousing_style_info']=$GLOBALS['db']->getRow($sql);
			
		//获取仓库信息
		$warehouse_id=$row['warehouse_id'];
		$sql="select * from ".$GLOBALS['ecs']->table('erp_warehouse')." where warehouse_id='".$warehouse_id."'";
		$row['warehouse_info']=$GLOBALS['db']->getRow($sql);
			
		//获取入仓操作者信息
		$create_by=$row['create_by'];
		$sql="select user_name from ".$GLOBALS['ecs']->table('admin_user')." where user_id='".$create_by."'";
		$row['admin_name']=$GLOBALS['db']->getOne($sql);
			
		//获取提交入库单操作者信息
		if(!empty($row['post_by']))
		{
			$post_by=$row['post_by'];
			$sql="select user_name from ".$GLOBALS['ecs']->table('admin_user')." where user_id='".$post_by."'";
			$row['post_by_name']=$GLOBALS['db']->getOne($sql);						
		}
		//获取审核入库单操作者信息
		if(!empty($row['approved_by']))
		{
			$approved_by=$row['approved_by'];
			$sql="select user_name from ".$GLOBALS['ecs']->table('admin_user')." where user_id='".$approved_by."'";
			$row['approved_by_name']=$GLOBALS['db']->getOne($sql);						
		}	
			
		//获取关联的订单信息
		if(!empty($row['order_id']))
		{
			$order_id=$row['order_id'];
			$sql="select order_sn,create_time,description,order_status from ".$GLOBALS['ecs']->table('erp_order')." where order_id='".$order_id."'";
			$row['order_info']=$GLOBALS['db']->getRow($sql);		
		}
			
		$warehousing_info[]=$row;
	}
	return $warehousing_info;	
}


/**
 * 锁定入仓单，出库单，以排斥的访问
 * 成功，返回true,失败返回false.
 *  * $tb,warehousing或delivery
 * $act=view,edit,delete,post_to_approve,approve.
 */

function lock_table($id='',$tb='',$act='')
{

	if(check_accessibility($id,$tb,$act))
	{	
		$user_id=erp_get_admin_id();
		
		$lock_time=time();
		$last_act_time=time();
	
		if($tb=='warehousing')
		{
	
			$sql="update ".$GLOBALS['ecs']->table('erp_warehousing')." set locked_by='".$user_id."'";
			$sql.=", locked_time='".$lock_time."'";
			$sql.=", last_act_time='".$last_act_time."'";
			$sql.=", is_locked='1'";
			$sql.=" where warehousing_id='".$id."'";
	
			$GLOBALS['db']->query($sql);
	
			return true;			
		}
		elseif($tb=='delivery')
		{
			$sql="update ".$GLOBALS['ecs']->table('erp_delivery')." set locked_by='".$user_id."'";
			$sql.=", locked_time='".$lock_time."'";
			$sql.=", last_act_time='".$last_act_time."'";
			$sql.=", is_locked='1'";
			$sql.=" where delivery_id='".$id."'";
	
			$GLOBALS['db']->query($sql);
	
			return true;			
		}

	}
	else{
		return false;
	}
}


/**
 * 返回入库单和出库单的有效操作
 */

function get_available_act($id='',$tb='')
{
	/*	warehousing_status=1  	表示正录入
	 *	warehousing_status=2  	审核中
	 *  warehousing_status=3  	审核通过
	 *  warehousing_status=4  	审核不通过
	*/
	
	$act['view']=check_accessibility($id,$tb,'view');
	$act['edit']=check_accessibility($id,$tb,'edit');
	$act['delete']=check_accessibility($id,$tb,'delete');
	$act['post_to_approve']=check_accessibility($id,$tb,'post_to_approve');
	$act['approve']=check_accessibility($id,$tb,'approve');
	
	return $act;
}

/**
 * 核对当前时间用户对某个出、入仓单是否能进行某项操作。Return true or false;
 * $id,warehousing_id或delivery_id
 * $tb,warehousing或delivery
 * $act=view,edit,delete,post_to_approve,approve.
 */

function check_accessibility($id='',$tb='',$act='')
{
	/*	warehousing_status=1  	表示正录入
	 *	warehousing_status=2  	审核中
	 *  warehousing_status=3  	审核通过
	 *  warehousing_status=4  	审核不通过
	*/
	
	if(empty($act) || empty($tb) || empty($id))
	{
		return false;
	}
	if($tb=='warehousing')
	{
		$sql="select status,is_locked,last_act_time,locked_by from ".$GLOBALS['ecs']->table('erp_warehousing')." where warehousing_id='".$id."'";
	}
	elseif($tb=='delivery')
	{
		$sql="select status,is_locked,last_act_time,locked_by from ".$GLOBALS['ecs']->table('erp_delivery')." where delivery_id='".$id."'";
	}
	else{
		return false;
	}
		
	$res=$GLOBALS['db']->getRow($sql);
		
	if($res==false)
	{
		return false;
	}
	else{
			
			$is_locked=$res['is_locked'];
			$status=$res['status'];
			$last_act_time=$res['last_act_time'];//最后的订单操作的时间，如果用户在10秒内无任何操作的时候，自动解除订单锁定。
			$locked_by=$res['locked_by'];	
		
			$current_time=time();
			$if_overtime=(($current_time-$last_act_time)>(10))? 1:0;//大于10秒，表示操作超时。
	
			$current_user_id=erp_get_admin_id();

			if($act=='view')
			{
				if(admin_priv('erp_warehouse_view', '', false) || admin_priv('erp_warehouse_manage', '', false) || admin_priv('erp_warehouse_approve', '', false))
				{
					return true;
				}
				else{
					return false;
				}				
			}
			elseif($act=='edit' || $act=='delete' || $act=='post_to_approve')
			{
				if($status==1)
				{
					if(admin_priv('erp_warehouse_manage', '', false) && ($is_locked==0 || ($is_locked==1 && $if_overtime==1 && $current_user_id!=$locked_by) || ($is_locked==1 && $current_user_id==$locked_by)))
					{
						return true;
					}
					else{
						return false;
					}
				}
				else{
					return false;
				}	
			}	
			elseif($act=='approve')
			{
				if($status==2)
				{
					if(admin_priv('erp_warehouse_approve', '', false) && ($is_locked==0 || ($is_locked==1 && $if_overtime==1 && $current_user_id!=$locked_by) || ($is_locked==1 && $current_user_id==$locked_by)))
					{
						return true;
					}
					else{
						return false;
					}				
				}
				else{
					return false;
				}
			}
			else{
				return false;
			}
	}

}

/*
 * 获取出入库完的销售订单信息
 */
function get_order_not_delivered()
{
	$sql="select distinct order_id from ".$GLOBALS['ecs']->table('order_goods')."where delivery_qty<goods_number";
	$res=$GLOBALS['db']->query($sql);
	while(list($order_id)=mysql_fetch_row($res))
	{
		$sql="select order_id,order_sn from ".$GLOBALS['ecs']->table('order_info')." where order_id='".$order_id."' and (shipping_status='1' or shipping_status='2') order by confirm_time";
		$result=$GLOBALS['db']->getRow($sql);
		if(!empty($result))
		{			
			$order_info[]=$result;
		}	
	}
	return $order_info;
}


/*
 * 获取所有通过审核但是未入库完的订单信息
 */
function get_order_not_warehoused()
{
	$sql="select distinct order_id from ".$GLOBALS['ecs']->table('erp_order_item')."where warehousing_qty<order_qty";
	$res=$GLOBALS['db']->query($sql);
	
	while(list($order_id)=mysql_fetch_row($res))
	{
		$sql="select order_id,order_sn,create_time,description,supplier_id from ".$GLOBALS['ecs']->table('erp_order')." where order_id='".$order_id."' and order_status='4'";
		$result=$GLOBALS['db']->getRow($sql);
		if(!empty($result))
		{			
			$order_info[]=$result;
		}	
	}
	return $order_info;
}


/*
 * 获取仓库信息
 */

function get_warehouse_info($warehouse_id='',$admin_id='',$is_valid=-1)
{
	if(empty($warehouse_id))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_warehouse')." where 1 ";

		if(!empty($admin_id))
		{
			$sql.=" and admin_id='".$admin_id."'";
		}
		
		if($is_valid!=-1)
		{
			$sql.=" and is_valid='".$is_valid."'";
		}
		
		$warehouse_info=$GLOBALS['db']->getAll($sql);
		
		if(!empty($warehouse_info))
		{
			foreach($warehouse_info as $key => $item)
			{
				$admin_id=$item['admin_id'];
				$admin_info=get_admin($admin_id);
				
				$warehouse_info[$key]['admin_name']=$admin_info['user_name'];
			}
		}
		
		return $warehouse_info;
	}
	else{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_warehouse');
		$sql.=" where warehouse_id='".$warehouse_id."'";
		
		return $GLOBALS['db']->getRow($sql);
	}
}


/*
 * 判断仓库是否存在，如存在返回仓库信息，否则返回false
 */
function is_warehouse_exists($warehouse_id='')
{
	if(empty($warehouse_id))
	{
		return false;
	}
	else{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_warehouse')." where warehouse_id='".$warehouse_id."'";
		$result=$GLOBALS['db']->getRow($sql);
		if($result!=false)
		{
			return $result;
		}
		else{
			return false;
		}
	}
}

function get_delivery_status()
{
	
	$delivery_status[0]['status_no']=1;
	$delivery_status[0]['status_style']="入库单录入中";

	$delivery_status[2]['status_no']=2;
	$delivery_status[2]['status_style']="主管审核中";

	$delivery_status[3]['status_no']=3;
	$delivery_status[3]['status_style']="审核已通过";

	$delivery_status[4]['status_no']=4;
	$delivery_status[4]['status_style']="审核未通过";
	
	return $delivery_status;	
}


function get_warehousing_status()
{
	
	$warehousing_status[0]['status_no']=1;
	$warehousing_status[0]['status_style']="入库单录入中";

	$warehousing_status[1]['status_no']=2;
	$warehousing_status[1]['status_style']="财务处理中";
	
	$warehousing_status[3]['status_no']=3;
	$warehousing_status[3]['status_style']="审核已通过";

	$warehousing_status[4]['status_no']=4;
	$warehousing_status[4]['status_style']="审核未通过";
	
	return $warehousing_status;	
}


/*
 * 获取入仓类型信息
 */

function get_warehousing_style_info($warehousing_style_id='',$is_valid='')
{
	if(empty($warehousing_style_id))
	{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_warehousing_style');
		
		if(!empty($is_valid))
		{
			$sql.=" where is_valid='".$is_valid."'";
		}
		
		return $GLOBALS['db']->getAll($sql);
	}
	else{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_warehousing_style');
		$sql.=" where warehousing_style_id='".$warehousing_style_id."'";
		
		return $GLOBALS['db']->getRow($sql);
	}
}

/*
 * 判断入仓类型是否存在，如存在返回入仓类型信息，否则返回false
 */
function is_warehousing_style_exists($warehousing_style_id='')
{
	if(empty($warehousing_style_id))
	{
		return false;
	}
	else{
		$sql="select * from ".$GLOBALS['ecs']->table('erp_warehousing_style')." where warehousing_style_id='".$warehousing_style_id."'";
		$result=$GLOBALS['db']->getRow($sql);
		if($result!=false)
		{
			return $result;
		}
		else{
			return false;
		}
	}
}
/*
 * 获取所有通过审核的订单的信息
 */
function get_order_info()
{
	$sql="select * from ".$GLOBALS['ecs']->table('erp_order')." where order_status='4'";//订单状态为4,表示审核通过
	return $GLOBALS['db']->getAll($sql);
}

/**
 * 产生出仓单号（出仓单号是形如'0911021'的7位字符串
 * 其中09表示2009年，11表示11月份，最后的三位是序号。0911021，表示09年11月的第21个出仓单。
 */

function gen_delivery_sn()
{
	$sql="select delivery_sn from ".$GLOBALS['ecs']->table('erp_delivery')." order by delivery_sn desc limit 1";
	$result=$GLOBALS['db']->getOne($sql);
	
	if(!empty($result))
	{
		$year_date=substr($result,2,4);
		$current_year_date=date("ym");
		if($current_year_date==$year_date)
		{
			$sn=substr($result,6,3);//获取后三位的序号
			$sn=intval($sn)+1;	//序号加1
			$sn=gen_zero_str(3-strlen($sn)).$sn;//加前导'0'
			$sn='DE'.$year_date.$sn;//加年，月
			
			return $sn;
		}
		else
		{
			$sn='DE'.$current_year_date."001";
			return $sn;
		}	
	}
	else{
		$current_year_date=date("ym");
		$sn='DE'.$current_year_date."001";
		return $sn;
	}
}

/**
 * 产生入仓单号（入仓单号是形如'WA0911021'的7位字符串
 * 其中WA是warehousing的前两个字母，09表示2009年，11表示11月份，最后的三位是序号。0911021，表示09年11月的第21个入仓单。
 */

function gen_warehousing_sn()
{
	$sql="select warehousing_sn from ".$GLOBALS['ecs']->table('erp_warehousing')." order by warehousing_sn desc limit 1";
	$result=$GLOBALS['db']->getOne($sql);
	
	if(!empty($result))
	{
		$year_date=substr($result,2,4);
		$current_year_date=date("ym");
		if($current_year_date==$year_date)
		{
			$sn=substr($result,6,3);//获取后三位的序号
			$sn=intval($sn)+1;	//序号加1
			$sn=gen_zero_str(3-strlen($sn)).$sn;//加前导'0'
			$sn='WA'.$year_date.$sn;//加年，月
			
			return $sn;
		}
		else
		{
			$sn='WA'.$current_year_date."001";
			return $sn;
		}	
	}
	else{
		$current_year_date=date("ym");
		$sn='WA'.$current_year_date."001";
		return $sn;
	}
}

function random_string($type = 'alnum', $len = 8)   
{                      
        switch($type)   
        {   
            case 'alnum'    :   
            case 'numeric'  :   
            case 'nozero'   :   
           
                    switch ($type)   
                    {   
                        case 'alnum'    :   $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';   
                            break;   
                        case 'numeric'  :   $pool = '0123456789';   
                            break;   
                        case 'nozero'   :   $pool = '123456789';   
                            break;   
                    }   
  
                    $str = '';   
                    for ($i=0; $i < $len; $i++)   
                    {   
                        $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);   
                    }   
                    return $str;   
              break;   
            case 'unique' : return md5(uniqid(mt_rand()));   
              break;   
        }   
   } 

?>